package com.xzy.cinematicket.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xzy.cinematicket.entity.User;
import com.xzy.cinematicket.service.IUserService;
import com.xzy.cinematicket.service.impl.UserServiceImpl;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/user")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IUserService userService = new UserServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("action");
		if("login".equals(method)){
			login(request, response);
		}else if("exit".equals(method)){
			exit(request, response);
		}else if("register".equals(method)){
			register(request, response);
		}else if("find".equals(method)){
			find(request, response);
		}else if("add".equals(method)){
			add(request, response);
		}else if("delete".equals(method)){
			delete(request, response);
		}
	}
	
	protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user = new User(0,request.getParameter("name"),request.getParameter("password"),null,0);
		HttpSession session = request.getSession();
		if("true".equals(session.getAttribute("flag"))){
			if((user = userService.find(user)) != null ){
				session.setAttribute("user", user);
				session.removeAttribute("action");
				session.removeAttribute("flag");
				if(user.getFlag() == 0)
					response.sendRedirect(request.getContextPath() + "/pages/index.jsp");
				else
					response.sendRedirect(request.getContextPath() + "/admin/index.jsp");
			}else{
				session.setAttribute("action", "fail");
				session.removeAttribute("flag");
				response.sendRedirect(request.getContextPath() + "/pages/login.jsp");
			}
		}else{
			session.setAttribute("action", "checkedFail");
			session.removeAttribute("flag");
			response.sendRedirect(request.getContextPath() + "/pages/login.jsp");
		}
	}
	
	protected void exit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(false);
		if(session != null)
			session.removeAttribute("user");
		response.sendRedirect(request.getContextPath() + "/pages/login.jsp");
	}
	
	protected void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("name");
		String flag = "true";
		if(userService.findByName(username))
			flag = "false";
		PrintWriter pw = response.getWriter();
		pw.write(flag);
		pw.flush();
		pw.close();
	}
	
	protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		if("true".equals(session.getAttribute("flag"))){
			User user = new User(0,request.getParameter("name"),request.getParameter("password"),request.getParameter("phone"),0);
			if(userService.add(user) > 0){
				session.removeAttribute("action");
				session.removeAttribute("flag");
				response.sendRedirect(request.getContextPath() + "/pages/login.jsp");
			}
		}else{
			session.setAttribute("action", "checkedFail");
			session.removeAttribute("flag");
			response.sendRedirect(request.getContextPath() + "/pages/register.jsp");
		}
	}
	
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user = new User(0,request.getParameter("name"),request.getParameter("password"),request.getParameter("phone"),Integer.parseInt(request.getParameter("flag")));
		userService.add(user);
		if(Integer.parseInt(request.getParameter("flag")) == 1)
			response.sendRedirect(request.getContextPath() + "/admin/addadmin.jsp");
		else
			response.sendRedirect(request.getContextPath() + "/admin/adduser.jsp");
	}
	
	protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		userService.deleteByName(request.getParameter("name"));
		if(Integer.parseInt(request.getParameter("flag")) == 1)
			response.sendRedirect(request.getContextPath() + "/admin/deleteadmin.jsp");
		else
			response.sendRedirect(request.getContextPath() + "/admin/deleteauser.jsp");
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
